package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.enums.RoleEnum;
import com.example.entity.Department;
import com.example.entity.Employee;
import com.example.entity.Meeting;
import com.example.mapper.MeetingMapper;
import com.example.service.DepartmentService;
import com.example.service.EmployeeService;
import com.example.service.MeetingService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/12/30 21:16
 * @Version 1.0.0
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements MeetingService {

    @Autowired
    private MeetingMapper meetingMapper;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 新增
     *
     * @param meeting
     */
    @Override
    public void add(Meeting meeting) {
        //获取当前登录的用户信息
        Employee currentUser = TokenUtils.getCurrentUser();
        //进行数据的处理
        if (currentUser.getRole().equals(RoleEnum.DEPARTMENT_MANAGER.name()) || currentUser.getRole().equals(RoleEnum.GENERAL_MANAGER.name())) {
            meeting.setDepartmentId(currentUser.getDepartmentId());
            meeting.setCreateId(currentUser.getId());
        }
        meetingMapper.insert(meeting);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        meetingMapper.deleteById(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            meetingMapper.deleteById(id);
        }
    }

    /**
     * 修改
     *
     * @param meeting
     */
    @Override
    public void updateMeeting(Meeting meeting) {
        meetingMapper.updateById(meeting);
    }

    /**
     * 根据ID查询
     *
     * @param id
     */
    @Override
    public Meeting selectById(Integer id) {
        return meetingMapper.selectById(id);
    }

    /**
     * 查询所有
     *
     * @param meeting
     */
    @Override
    public List<Meeting> selectAll(Meeting meeting) {
        LambdaQueryWrapper<Meeting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Meeting> meetingList = meetingMapper.selectList(lambdaQueryWrapper);
        meetingList.forEach( item ->{
            //将会议的创建人和参会人员进行处理
            Employee employee = employeeService.selectById(item.getCreateId());
            Integer departmentId = item.getDepartmentId();
            if (departmentId == 0) {
                item.setDepartmentName("所有员工");
            }else {
                item.setDepartmentName(employee.getDepartmentName()+"所有员工");
            }
            item.setCreateName(employee.getName());
        });
        return meetingList;
    }

    /**
     * 分页查询
     *
     * @param meeting
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<Meeting> selectPage(Meeting meeting, Integer pageNum, Integer pageSize) {
        Employee user = TokenUtils.getCurrentUser();
        LambdaQueryWrapper<Meeting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Meeting::getId);
        lambdaQueryWrapper.like(ObjectUtil.isNotEmpty(meeting.getName()),Meeting::getName,meeting.getName());
        //员工和部门只能查看自己部门的会议信息
        if (user.getRole().equals(RoleEnum.USER.name()) || user.getRole().equals(RoleEnum.DEPARTMENT_MANAGER.name())) {
            lambdaQueryWrapper.and( wq ->{wq
                    .eq(Meeting::getDepartmentId,user.getDepartmentId())
                    .or()
                    .eq(Meeting::getDepartmentId,0);
            });
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Meeting> meetingList = meetingMapper.selectList(lambdaQueryWrapper);
        meetingList.forEach( item ->{
            //将会议的创建人和参会人员进行处理
            Employee employee = employeeService.selectById(item.getCreateId());
            Integer departmentId = item.getDepartmentId();
            if (departmentId == 0) {
                item.setDepartmentName("所有员工");
            }else {
                Integer departmentId1 = employee.getDepartmentId();
                Department department = departmentService.selectById(departmentId1);
                item.setDepartmentName(department.getDepartName()+"所有员工");
            }
            item.setCreateName(employee.getName());
        });
        return PageInfo.of(meetingList);
    }
}
